package com.spdg.ring.adapter;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import cn.wolf.tools.StringUtil;

import com.spdg.ring.R;
import com.spdg.ring.sqlite.model.ArticleList;
import com.spdg.ring.util.ImageLoaderGrid;

public class GiftListAdapter extends BaseAdapter {
	private List<ArticleList> mContentList = null;
	private Context mContext;
	private final LayoutInflater mInflater;

	public GiftListAdapter(Context ctx, List<ArticleList> list) {
		this.mContext = ctx;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (list == null) {
			mContentList = Collections.emptyList();
		}

		this.mContentList = list;
	}

	@Override
	public int getCount() {
		return mContentList.size();
	}

	@Override
	public Object getItem(int position) {
		return mContentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.listview_gift_item, null);
		}

		ImageView imgView = (ImageView) convertView.findViewById(R.id.gift_list_img);
		TextView titleView = (TextView) convertView.findViewById(R.id.gift_list_title);
		TextView contentView = (TextView) convertView.findViewById(R.id.gift_list_content);

		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		imgView.setLayoutParams(params);

		ArticleList entity = mContentList.get(position);

		if (entity != null) {
			String imgUrl = entity.getIcon();
			imgView.setImageResource(R.drawable.default_image_big);
			ImageLoaderGrid.getInstance().loadBitmapOvri(mContext, imgView, imgUrl,
					entity.getIconFile(), R.drawable.default_image_big);

			String title = entity.getName();
			if (StringUtil.isEmpty(title))
				titleView.setText("");
			titleView.setText(Html.fromHtml(title));

			String summary = entity.getSummary();
			if (StringUtil.isEmpty(summary))
				contentView.setText("");
			contentView.setText(Html.fromHtml(summary));
		}

		return convertView;
	}

}
